<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Heart Shape with Canvas</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        canvas {
            background-color: #fff;
        }
    </style>
</head>
<body>
    <canvas id="heartCanvas"></canvas>
    <script>
        function drawHeart() {
            const canvas = document.getElementById('heartCanvas');
            const ctx = canvas.getContext('2d');

            // 设置画布大小
            const size = 400;
            canvas.width = size;
            canvas.height = size;

            // 心形方程参数
            const a = size / 4;

            ctx.beginPath();
            for (let theta = 0; theta < Math.PI * 2; theta += 0.01) {
                const x = a * 16 * Math.sin(theta) ** 3;
                const y = -a * (13 * Math.cos(theta) - 5 * Math.cos(2 * theta) - 2 * Math.cos(3 * theta) - Math.cos(4 * theta));
                const newX = x + size / 2;
                const newY = y + size / 1.5;
                if (theta === 0) {
                    ctx.moveTo(newX, newY);
                } else {
                    ctx.lineTo(newX, newY);
                }
            }
            ctx.closePath();
            ctx.fillStyle = 'red';
            ctx.fill();
        }

        drawHeart();
    </script>
</body>
</html>